package com.p2p.search.dao.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.annotation.Resource;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.p2p.common.utils.StringUtils;

import com.p2p.search.dao.SearchDao;
import com.p2p.search.po.Netbar;
import com.p2p.search.vo.SelectUser;

@Repository
public class SearchDaoImpl implements SearchDao {
	@Resource
	private JdbcTemplate jdbcTemplate;

	@Override
	public List<Netbar> queryByCondition(SelectUser selectUser) {
		RowMapper<Netbar> rowMapper = new BeanPropertyRowMapper<>(Netbar.class);
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = formatter.format(currentTime);
		String bDate_time = selectUser.getBdate_time();
		String eDate_time = selectUser.getEdate_time();
		String cmac = selectUser.getCmac();
		String mac = selectUser.getMac();
		String ip = selectUser.getIp();
		String cmacsql = null;
		String macsql = null;
		String ipsql = null;
		String date = null;
		if (StringUtils.isNotEmpty(cmac)) {
			cmacsql = "and  cmac='" + cmac + "'";
		} else {

			cmacsql = "and cmac=cmac";
		}

		if (StringUtils.isNotEmpty(mac)) {
			macsql = "and  mac='" + mac + "'";
		} else {

			macsql = "and mac=mac";
		}
		if (StringUtils.isNotEmpty(ip)) {
			ipsql = "and  ip='" + ip + "'";
		} else {

			ipsql = "and ip=ip";
		}
		if (StringUtils.isNotEmpty(bDate_time)) {
			date = " date_time BETWEEN '" + bDate_time + "' AND '" + eDate_time + "'";
		} else {
			String timesql = "SELECT Max(date_time) as date_time FROM `p2p_mac_"
					+ dateString.replace("-", "").substring(0, 6) + "` ";
			Map map2 = (Map) (jdbcTemplate.queryForList(timesql).get(0));
			date = " date_time='" + map2.get("date_time").toString() + "'";
		}
		String sql = "select * from p2p_mac_" + date.replace("-", "").substring(0, 6) + " where " + date + ipsql
				+ macsql + cmacsql;
		List<Netbar> list = jdbcTemplate.query(sql, rowMapper);
		
		
		
		return list;
	}

}
